#include "config_main.h"
#ifdef DDENN_TIMERS
/*
 * timers.c
 *
 *  Created on: 06.06.2009
 *      Author: Admin
 */
#include "timers.h"

/**************************************************************************************/
//
//          it provides ddenn/timers/
//
/*************************************************************************************/


DdennTimers::ddenn_timer DdennTimers::DDennTimers[DDENN_TIMERS_COUNT];


void DdennTimers::RegisterDdennTimers(){
	for (int i=0; i<(DDENN_TIMERS_COUNT); ++i){
		dden_timer_create(&(DDennTimers[i]));
		XPLMRegisterDataAccessor(  StPrintI( "ddenn/timers/timer%d_hours", i) ,    xplmType_Int, 0,
			GetTimerHours,
			NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
		XPLMRegisterDataAccessor(StPrintI( "ddenn/timers/timer%d_minutes", i)  ,    xplmType_Int, 0,
			GetTimerMinutes,
			NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
		XPLMRegisterDataAccessor( StPrintI(  "ddenn/timers/timer%d_seconds", i)   ,    xplmType_Int, 0,
			GetTimerSeconds,
			NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
		XPLMRegisterDataAccessor(StPrintI(  "ddenn/timers/timer%d_raw", i)   ,    xplmType_Float, 1,NULL,NULL,
			GetTimerRaw,
			SetTimerRaw,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
		XPLMRegisterDataAccessor(StPrintI( "ddenn/timers/timer%d_pause", i)   ,    xplmType_Int, 1,
			GetTimerPause,
			SetTimerPause,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
		XPLMRegisterDataAccessor(StPrintI(   "ddenn/timers/timer%d_clear", i) ,    xplmType_Int, 1,
			GetTimerClear,
			SetTimerClear,NULL,NULL,NULL, NULL,NULL,NULL,NULL,  NULL,NULL,    NULL,  &(DDennTimers[i]), &(DDennTimers[i]));
	}
}


void DdennTimers::UnregisterDdennTimers(){
	for (int i=0; i<(DDENN_TIMERS_COUNT); ++i){
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI(  "ddenn/timers/timer%d_hours", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI( "ddenn/timers/timer%d_minutes", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI("ddenn/timers/timer%d_seconds", i) ));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI(  "ddenn/timers/timer%d_raw", i)));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI("ddenn/timers/timer%d_pause", i) ));
		XPLMUnregisterDataAccessor( XPLMFindDataRef(StPrintI("ddenn/timers/timer%d_clear", i) ));
	}
}



void DdennTimers::dden_timer_create (ddenn_timer *  t)
{
	(*t).starttime = 0;
	(*t).pausetime = 0;
	(*t).pausedtime = 0;
	(*t).ispaused = 1;
};


void DdennTimers::dden_timer_reset (ddenn_timer *  t)
{
	(*t).starttime = GetXPlaneLocalTime();
	(*t).pausetime = GetXPlaneLocalTime();
	(*t).pausedtime = 0;
	(*t).ispaused = 1;
};


void DdennTimers::ddenn_timer_pause (ddenn_timer *  t)
{
	(*t).pausetime = GetXPlaneLocalTime();
	(*t).ispaused = 1;
};


void DdennTimers::ddenn_timer_play (ddenn_timer *  t)
{
	(*t).ispaused = 0;
	(*t).pausedtime =(*t).pausedtime + (GetXPlaneLocalTime() - ((*t).pausetime));

};


float DdennTimers::ddenn_timer_gettime (ddenn_timer  t)
{
  if (t.ispaused){

	  return( (t.pausetime)-(t.pausedtime) -(t.starttime));
  }
  else{

	  return( (GetXPlaneLocalTime()) -(t.starttime) - (t.pausedtime));
  }
};



int DdennTimers::GetTimerHours ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return UTCTimeToHours(ddenn_timer_gettime(*p));
}
int DdennTimers::GetTimerMinutes ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return UTCTimeToMinutes(ddenn_timer_gettime(*p));
}

int DdennTimers::GetTimerSeconds ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return UTCTimeToSeconds(ddenn_timer_gettime(*p));
}


float DdennTimers::GetTimerRaw ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	return ddenn_timer_gettime(*p);
}




void DdennTimers::SetTimerRaw ( void *               inRefcon,float inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;


    (*p).starttime=GetXPlaneLocalTime();
    (*p).pausedtime=0;
    (*p).pausetime=GetXPlaneLocalTime()+inValue;
    (*p).ispaused=true;
}



void DdennTimers::SetTimerClear(void *               inRefcon,
                 int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if (inValue) {dden_timer_reset((p));} else{};
}


int DdennTimers::GetTimerClear ( void *               inRefcon){return 0;}



int DdennTimers::GetTimerPause ( void *               inRefcon)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if ((*p).ispaused) {return 1;} else {return 0;};
}


void DdennTimers::SetTimerPause(void *               inRefcon,
                 int                  inValue)
{
	ddenn_timer *p = (ddenn_timer*)inRefcon;
	if (inValue) {
		if ((*p).ispaused){
			ddenn_timer_play((p));
		}else{
			ddenn_timer_pause((p));
		}
	};
}



/**************************************************************************************/
//
//          end of ddenn/timers/
//
/*************************************************************************************/


#endif
